package cjq.demo.lambda;

import java.util.function.Consumer;

//3、Consumer与Loan Pattern
//比如我们有一个资源类Resource：
public class Resource {
	public Resource() {  
        System.out.println("Opening resource");  
    }  
 
    public void operate() {  
        System.out.println("Operating on resource");  
    }  
 
    public void dispose() {  
        System.out.println("Disposing resource");  
    }
    /**
     * Java 8提供了一个Consumer接口，代码改写为如下：
     */
    //Java 8提供了一个Consumer接口
    public static void withResource(Consumer<Resource> consumer) {  
        Resource resource = new Resource();  
        try {  
            consumer.accept(resource);  
        } finally {  
            resource.dispose();  
        }  
    }  
}
